package cn.bgs.controller;


import cn.bgs.common.AliOss;
import cn.bgs.pojo.PersonnelInfo;
import cn.bgs.service.PersonnelInfoService;
import cn.bgs.common.R;
import cn.bgs.util.IpAddress;
import cn.bgs.util.IpUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 乡镇人员信息表 前端控制器
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@RestController
@RequestMapping("/personnel-info")
public class PersonnelInfoController {

    @Autowired
    private PersonnelInfoService personnelInfoService;

    @Autowired
    private AliOss aliOss;


    @GetMapping("/villagepersonnelinfo")
    @ApiOperation(value = "乡镇人员信息查询")
    public R projectManageInfo(@RequestParam Integer pageNum,
                               @RequestParam Integer pageSize,
                               @RequestParam(defaultValue = "") String name,
                               @RequestParam(defaultValue = "") String workDomain){
        QueryWrapper<PersonnelInfo> queryWrapper= new QueryWrapper<>();
        queryWrapper.like("name",name);
        queryWrapper.like("work_domain",workDomain);
        queryWrapper.orderByDesc("create_time").eq("delete_flag",0);
        Page<PersonnelInfo> page = personnelInfoService.page(new Page<>(pageNum, pageSize),queryWrapper);
        return R.ok().data("villagepersonnelinfo" , page);
    }
    @RequestMapping("/addPic")
    @ApiOperation(value = "项目图片")
    public String addPic(MultipartFile file){
        String upload = aliOss.upload(file);
        System.out.println(upload);
        return upload;
    }

    @RequestMapping("/updatedInfo")
    @ApiOperation(value = "乡镇人员信息修改")
    public R updatedInfo(@RequestBody PersonnelInfo personnelInfo){

        boolean save = personnelInfoService.updatedInfo(personnelInfo);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }
    @RequestMapping("/addInfo")
    @ApiOperation(value = "乡镇人员信息添加")
    public R addInfo(@RequestBody PersonnelInfo personnelInfo){

        boolean save = personnelInfoService.addInfo(personnelInfo);
        if (save){
            return R.ok();
        }else {
            return R.error();
        }


    }

        @RequestMapping("/deleteInfo/{id}")
        @ApiOperation(value = "乡镇人员信息删除")
        public R addInfo(@PathVariable Integer id){

            boolean save = personnelInfoService.removeById(id);
            if (save){
                return R.ok();
            }else {
                return R.error();
            }


        }

    @GetMapping("/ipAddress")
    public R ipAddress() throws IOException {
        String address = "";
        String ipv4 = IpUtil.getOutIPV4();
        // 腾讯地图申请的ak
        String key = "Z4HBZ-TMML2-G5GUF-CQ2AK-224TH-U4FYZ";
        JSONObject json = IpAddress.readJsonFromUrl("https://apis.map.qq.com/ws/location/v1/ip?ip="+ipv4+"&key="+key);
        String result = json.get("result").toString();
        JSONObject jsonObject = JSONObject.parseObject(result);
        String ad_info = jsonObject.get("ad_info").toString();
        JSONObject jsonObject1 = JSONObject.parseObject(ad_info);
        address += jsonObject1.get("nation").toString();
        address += jsonObject1.get("province").toString();
        address += jsonObject1.get("city").toString();

        return R.ok().data("ipv4" , ipv4).data("ipAddress" , json).data("address" , address);
    }


    @GetMapping("/villagepersonnelcount")
    @ApiOperation(value = "乡镇人员注册查询")
    public R villagepersonnelcount(){
        QueryWrapper<PersonnelInfo> queryWrapper= new QueryWrapper<>();

        queryWrapper.eq("delete_flag",0).gt("id",0);
       Integer vpc= personnelInfoService.seletecount(queryWrapper);

        return R.ok().data("villagepersonnelcount" , vpc);
    }



}

